﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Data;
using ncp.Business;
using ncp.Business.Common;
using ncp.Business.SingleManager;

namespace ncp.Web.cms.work.SingleManager
{
    public partial class Single_Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
            if (userInfo == null)
            {
                Response.Write(" <script> top.location.href='/cms/login.aspx'; </script> ");
            }
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Single_Index));
        }

        //得到总记录数
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public int GetRecordCount()
        {
            return SinglePageBll.GetSingleCount("", CommonConst.DB_Name);
        }

        //装载商品表视图
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string BindTable(string jsonData, string jsonWhere)
        {
            var dictList = JsonHelper.JSONToObject<Dictionary<string, object>>(jsonData);
            var dicWhere = new Dictionary<string, object>();
            DataTable dt = SinglePageBll.GetSingleTable(int.Parse(dictList["currentpageindex"].ToString()) - 1, int.Parse(dictList["pagesize"].ToString()), "*", dictList["sorting"] + " " + dictList["sortdir"], dicWhere, CommonConst.DB_Name);
            var json = JsonHelper.DataTableToJSON(dt);
            return json;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string OnSave(string json)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
            if (userInfo != null)
            {
                try
                {
                    var dicProj = JsonHelper.JSONToObject<Dictionary<string, object>>(json);
                    var className = dicProj["className"].ToString();
                    var content = dicProj["Single_Content"].ToString();
                    var path = dicProj["path"].ToString();
                    var temPath = dicProj["temPath"].ToString();
                    dicProj.Remove("temPath");
                    dicProj.Remove("path");
                    dicProj.Remove("className");
                    SinglePageBll.InsertSingle(dicProj, CommonConst.DB_Name);
                    var reader = new System.IO.StreamReader(Server.MapPath(temPath) , System.Text.Encoding.UTF8);
                    var template = reader.ReadToEnd();
                    var newHtml = template.Replace("{@title}", className).Replace("{@content}", content);
                    using (StreamWriter sw = new StreamWriter(Server.MapPath(path), false, System.Text.Encoding.UTF8))
                    {
                        sw.WriteLine(newHtml);
                        sw.Flush();
                        sw.Close();
                    }
                    jsonResult.Add("isreload", true);
                    jsonResult.Add("msg", "保存成功!");
                }
                catch (Exception ex)
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }

            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //删除商品信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string DeleteNewsInfo(int newsId)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                int ii = SinglePageBll.DeleteSingle(newsId.ToString(), CommonConst.DB_Name);
                jsonResult.Add("msg", "删除成功！");
                jsonResult.Add("isreload", true);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //修改文章状态
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string SetNewsStatus(int newsId, int status)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            Dictionary<string, object> dicNews = new Dictionary<string, object>();
            Dictionary<string, object> dicWhere = new Dictionary<string, object>();
            dicWhere.Add("Link_Id", newsId);
            dicNews.Add("Link_Status", status);
            try
            {
                SinglePageBll.UpdateSingle(dicNews, dicWhere, CommonConst.DB_Name);
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", "修改成功!");
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
